package cn.czh.basic.service.impl;

import cn.czh.basic.exception.BusinessException;
import cn.czh.basic.service.IBaiduAiAuditService;
import cn.czh.basic.util.BaiduAiUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class BaiduAiAuditServiceImpl implements IBaiduAiAuditService {


    @Override
    public Boolean textAudit(String auditText) {
        //@TODO - 自主研发 - 工具类BaiduAuditUtils
        if (!BaiduAiUtils.textCensor(auditText)) {
            throw new BusinessException("你输入的名称违规,请重新输入");
        }
        return true;
    }

    /*
     *  注意:resources是这样的格式：
     *  /group1/M00/00/02/CgAIC2KevEeAX2T4AAEUusLjqqk161.png,/group1/M00/00/02/CgAIC2KevEeAX2T4AAEUusLjqqk162.png
     *  需要判断是否为空，只有一张怎么处理，有多张怎么处理
     *  需要统一加前缀：http://123.207.27.208
     */
    @Override
    public Boolean imageAudit(String resources) {
        //@TODO - 自主研发 - 工具类BaiduAuditUtils
        //图片为空的时候
        if (StringUtils.isEmpty(resources)) {
            throw new BusinessException("图片不能为空");
        } else {
            String[] split = resources.split(",");
            String prefix = "http://123.207.27.208";
            //处理一张图片的时候
            if (split.length==1 && !BaiduAiUtils.imgCensor(prefix+split[0])){
                throw new BusinessException("该图片违规,请重新上传");
            }
            //多图处理
            for (int i = 0; i < split.length; i++) {
                String imgUrl = prefix + split[i];
                if (!BaiduAiUtils.imgCensor(imgUrl)) {
                    throw new BusinessException("第" + (i + 1) + "张图片违规，请删除或重新上传");
                }
            }
        }
        return true;
    }
}